package com.goldstone.actions;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.dbb.util.StringUtils;
import com.goldstone.entity.Attachment;
import com.goldstone.entity.Color;
import com.goldstone.entity.Company;
import com.goldstone.entity.PriceLog;
import com.goldstone.entity.ProDesc;
import com.goldstone.entity.Product;
import com.goldstone.entity.Size;
import com.goldstone.entity.code.PriceType;
import com.goldstone.service.AttachmentService;
import com.goldstone.service.CompanyService;
import com.goldstone.service.ProductService;
import com.goldstone.service.TagService;

@Namespace("/product")
@Results( { @Result(name = CrudActionSupport.RELOAD, location = "product.do", type = "redirect") })
public class ProductAction extends CrudActionSupport<Product> {
	
	private static final long serialVersionUID = -6456145401441423430L;
	
	@Autowired
	ProductService service;
	
	@Autowired
	CompanyService companyService;
	
	@Autowired
	AttachmentService attaService;
	
	@Autowired
	TagService tagService;
	private Product product;
	private String[] descs;
	private String[] enDescs;
	private String[] procolor;
	private String[] prosize;
	private String images;
	private String id;
	private String dialogType;
	private String tagsString;
	private String str;
	private PriceLog priceLog;
	
	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		filters.add(new PropertyFilter("EQB_deleted", "false"));
		service.find(getPage(), filters);
		return SUCCESS;
	}
	@Override
	public String input() throws Exception {
		logger.warn("input");
		return INPUT;
	}
	@Override
	public String save() throws Exception {
		for(int i = 0; i < descs.length; i++) {
			if(descs[i].trim().length() > 0)
				product.addDesc(new ProDesc(descs[i], enDescs[i]));
		}
		for(String color : procolor) {
			if(color.trim().length() > 0)
				product.addColor(new Color(color));
		}
		for(String size : prosize) {
			if(size.trim().length() > 0)
				product.addSize(new Size(size));
		}
		List<Attachment> pics = getImageList();
		if(pics.size() > 0)
			product.setLogo(pics.get(0));
		System.out.println("Pics Size::" + pics.size());
		product.setPics(pics);
		product.setTags(tagService.saveTagString(tagsString));
		if(product.getRef().trim().length() > 0)
			service.save(product);
		return RELOAD;
	}
	
	public void getStr() {
		product = service.get(id);
		if(product != null) {
			if("comment".equals(dialogType)) {
				//Struts2Utils.renderText(product.getComment());
			} else if("tags".equals(dialogType)) {
				Struts2Utils.renderText(product.getTagsString());
			}
		}
	}
	
	public void addStr() {
		product = service.get(id);
		if(product != null) {
			if("comment".equals(dialogType)) {
				product.addProComment(str);
				service.save(product);
				Struts2Utils.renderText("success");
			} else if("tags".equals(dialogType)) {
				product.setTags(tagService.saveTagString(str));
				service.save(product);
				Struts2Utils.renderText("success");
			}
		}
	}
	
	public List<Attachment> getImageList() {
		List<Attachment> attas = new ArrayList<Attachment>();
		String[] imgIds = StringUtils.getArrayFromString(images, ",");
		for(String id : imgIds) {
			Attachment atta = attaService.get(id);
			if(atta != null)
				attas.add(atta);
		}
		return attas;
	}
	
	@Override
	public String delete() throws Exception {
		service.delete(id);
		return list();
	}
	@Override
	protected void prepareModel() throws Exception {
		if(id != null) {
			product = service.get(id);
		} else {
			product = new Product();
		}
	}
	
	public void addPrice() {
		service.savePrice(id, priceLog);
	}
	
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}
	public String[] getDescs() {
		return descs;
	}
	public void setDescs(String[] descs) {
		this.descs = descs;
	}
	public String[] getEnDescs() {
		return enDescs;
	}
	public void setEnDescs(String[] enDescs) {
		this.enDescs = enDescs;
	}
	public String[] getProcolor() {
		return procolor;
	}
	public void setProcolor(String[] procolor) {
		this.procolor = procolor;
	}
	public String[] getProsize() {
		return prosize;
	}
	public void setProsize(String[] prosize) {
		this.prosize = prosize;
	}
	public String getImages() {
		return images;
	}
	public void setImages(String images) {
		this.images = images;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getDialogType() {
		return dialogType;
	}
	public void setDialogType(String dialogType) {
		this.dialogType = dialogType;
	}
	public void setStr(String str) {
		this.str = str;
	}
	public String getTagsString() {
		return tagsString;
	}
	public void setTagsString(String tagsString) {
		this.tagsString = tagsString;
	}
	
	public List<Company> getCompanys() {
		return companyService.findAll();
	}
	public List<PriceType> getPriceTypes() {
		List<PriceType> pts = new ArrayList<PriceType>();
		for(PriceType pt : PriceType.values())
			pts.add(pt);
		return pts;
	}
	public PriceLog getPriceLog() {
		return priceLog;
	}
	public void setPriceLog(PriceLog priceLog) {
		this.priceLog = priceLog;
	}
	
	
}
